package Android.DrawLock;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Start_lock extends Activity
{
  private String gesPath;
  private File gesFile;
  private GestureLibrary gesLib;
  private GestureOverlayView gesOverlay;

  private LinearLayout myLinearLayout;
  /* LayoutInflater�拐辣雿�啣遣AlertDialog銋 */
  private LayoutInflater mInflater01;
  /* 頛詨閫���iew */
  private View mView01;
  private EditText mEditText01;
  private String password;
  private int openness;
  private SharedPreferences settings;
  public static final String TAG = "LockMediator";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    /* �澆�刻撟＊蝷�*/
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    settings = getSharedPreferences("drawlockSetting", 0);
    password = settings.getString("passwd", "");
    openness = settings.getInt("openness", 128);

    setContentView(R.layout.startlock);
    setUpUIComponent();
    setUpListener();
    setGesture();
  }

  @Override
  protected void onPause()
  {
    Log.e(TAG, "onPause");
    if (Stamp_Service.lock == true)
    {      
      Start_lock.this.finish();
      // �嗆�訌eceiver������甇斤�撘�     
      Intent mIntent = new Intent(this, Start_lock.class);
      // 閮剖�Intent���慚LAG_ACTIVITY_NEW_TASK
      mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      // 撠ntent隞叫tartActivity�喲�蝯虫�璆剔頂蝯�      
      this.startActivity(mIntent);
    }
    super.onPause();
  }

  @Override
  protected void onDestroy()
  {
    // TODO Auto-generated method stub
    Log.e(TAG, "onDestroy ");
    super.onDestroy();
  }

  // set�批��  
  private void setUpUIComponent()
  {
    myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
    /* GestureOverlayView����*/
    gesOverlay = (GestureOverlayView) findViewById(R.id.myGestures1);
  }

  // set�批��Listener�vent
  private void setUpListener()
  {
    // 閮剖���蜓�恍��摨�   
    myLinearLayout.setBackgroundColor(Color.argb(openness, 0, 0, 0));

    gesOverlay.addOnGesturePerformedListener(new MyListener(this));
  }

  private void setGesture()
  {
    gesPath = new File(getFilesDir(), "drawlock").getAbsolutePath();
    gesFile = new File(gesPath);
    gesLib = GestureLibraries.fromFile(gesFile);

    if (!gesLib.load())
    {
      // 霈��憭望�嚗＊蝷摭oast閮
      Toast.makeText(Start_lock.this, R.string.drowfile_read_error,
          Toast.LENGTH_LONG).show();
    }
  }

  public boolean onCreateOptionsMenu(Menu menu)
  {
    menu.add(0, 0, 0, R.string.unlock);
    return super.onCreateOptionsMenu(menu);
  }

  public boolean onOptionsItemSelected(MenuItem item)
  {
    try
    {
      /* LayoutInflater.from��甇乙ctivity�ontext */
      mInflater01 = LayoutInflater.from(Start_lock.this);

      /* 撱箇�閫��撖Ⅳ雿輻View�ayout */
      mView01 = mInflater01.inflate(R.layout.securescreen, null);

      /* �澆�閰望憛葉�臭��ditText蝑�頛詨閫��撖Ⅳ */
      mEditText01 = (EditText) mView01.findViewById(R.id.myEditText2);

      /* 撱箇�AlertDialog */
      new AlertDialog.Builder(this)
          .setView(mView01)
          .setPositiveButton(R.string.enter,
              new DialogInterface.OnClickListener()
              {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                  /* 瘥�頛詨��蝣潸��ctivity鋆∠�閮剖��臬�貊泵 */
                  if (password.equals(mEditText01.getText().toString()))
                  {
                    /* �嗅�蝣潭迤蝣箸���閫�� */
                    Start_lock.this.finish();
                    Stamp_Service.lock = false;
                  }
                }
              })
          .setNegativeButton(R.string.btn_exit,
              new DialogInterface.OnClickListener()
              {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                  dialog.dismiss();
                }
              }).show();
    } catch (Exception e)
    {
      e.printStackTrace();
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public boolean onKeyDown(int key, KeyEvent event)
  {
    switch (key)
    {
      case KeyEvent.KEYCODE_SEARCH:
        Start_lock.this.finish();
        Intent mIntent = new Intent(this, Start_lock.class);
        /* 閮剖�Intent���慚LAG_ACTIVITY_NEW_TASK */
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        /* 撠ntent隞叫tartActivity�喲�蝯虫�璆剔頂蝯�*/
        this.startActivity(mIntent);
        break;
      case KeyEvent.KEYCODE_BACK:
        break;
      default:
        return false;
    }
    return false;
  }

  /* �芸�蝢呢nGesturePerformedListener */
  public class MyListener implements OnGesturePerformedListener
  {
    private Context context;
    // ��雿輻�身摰�    
    private SharedPreferences settings = getSharedPreferences(
        "drawlockSetting", 0);
    private double ges_clever;

    public MyListener(Context context)
    {
      this.context = context;
    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
    {
      ges_clever = Double.parseDouble(settings.getString("clever", null));
      try
      {
        /* �瘥� */
        ArrayList<Prediction> predictions = gesLib.recognize(gesture);
        if (predictions.size() > 0)
        {
          /* ���亥�����*/
          Prediction prediction = predictions.get(0);
          /* ���葫�潸撠之��.0 */
          if (prediction.score > ges_clever)
          {
            Start_lock.this.finish();
            Stamp_Service.lock = false;
          } else
          {
            // 瘥�銝嚗＊蝷摭oast
            Toast.makeText(MyListener.this.context, R.string.no_gesture,
                Toast.LENGTH_SHORT).show();
          }
        } else
        {
          // 瘥�銝嚗＊蝷摭oast
          Toast.makeText(MyListener.this.context, R.string.no_gesture,
              Toast.LENGTH_SHORT).show();
        }
      } catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
}
